package dao.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import model.Activitate;
import model.AsistentSocial;
import dao.ActivitateDao;
import dao.DaoException;

public class ActivitateDaoImpl extends Dao implements ActivitateDao {

	@Override
	public void addOrUpdateActivitate(Activitate activitate) {
		hibernateTemplate.saveOrUpdate(activitate);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Set<Activitate> getAllActivitati() {
		List<Activitate> res = hibernateTemplate.loadAll(Activitate.class);
		return new HashSet<Activitate>(res);
	}

	@Override
	public void removeActivitate(int id) throws DaoException {
		hibernateTemplate.delete(this.getActivittateByID(id));
		
	}

	@Override
	public Activitate getActivittateByID(int id) throws DaoException {
		DetachedCriteria crit = DetachedCriteria.forClass(Activitate.class).add(Restrictions.eq("id", id));
        @SuppressWarnings("rawtypes")
		List res = hibernateTemplate.findByCriteria(crit);
        if (res.size() == 0) {
            throw new DaoException("Nu s-a gasit nici un asistent cu acest id");
        }
       
        return (Activitate) res.get(0);
	}

	@Override
	public int getMaxId() {
		List res= hibernateTemplate.find("select max(activitateId) from Activitate");
		if( res.get(0) == null || res.isEmpty() ) return 0;
		return Integer.parseInt(res.get(0).toString());
	}
	
}
